package com.mdinic.blogovi.front.common.login;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.cookies.CookieUtils;

import com.mdinic.blogovi.front.common.IReturn;
import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.front.page.forgotpass.ForgotPasswordPage;
import com.mdinic.blogovi.service.PersonService;
import com.mdinic.blogovi.service.model.LoginResult;

public class LoginWindow extends Panel {

    private static final String AUTH_CODE_COOKIE = "bauth";

    private static final long serialVersionUID = 8894842372171823213L;

    @SpringBean
    private transient PersonService personService;

    public LoginWindow(AjaxRequestTarget target, String id, final IReturn iReturn) {
        super(id);

        final Credentials credentials = new Credentials();

        Form<Credentials> form = new Form<Credentials>("form", new CompoundPropertyModel<Credentials>(credentials));

        add(form);

        final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        feedbackPanel.setMarkupId("login_window_feedback");
        form.add(feedbackPanel);
        TextField<String> email = new TextField<String>(Credentials.EMAIL);
        email.setOutputMarkupId(true);
        email.setMarkupId("email");
        email.setRequired(true);
        form.add(email);

        PasswordTextField passwordTextField = new PasswordTextField(Credentials.PASSWORD);
        passwordTextField.setRequired(true);
        passwordTextField.setOutputMarkupId(true);
        passwordTextField.setMarkupId("pass");
        form.add(passwordTextField);

        final IModel<Boolean> remember = new Model<Boolean>(true);

        form.add(new AjaxButton("ok") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                super.onError(target, form);
                target.add(feedbackPanel);
            }

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                LoginResult login = personService.login(credentials);
                switch (login.getType()) {
                case FAIL:
                    error("Pogresna kombinacija E-maila i lozinke.");
                    target.add(feedbackPanel);
                    break;
                case NOT_ACTIVATED:
                    error("Vas nalog nije aktivan. Nalog mozete aktivirati klikom na link u aktivacionom e-mailu.");
                    target.add(feedbackPanel);
                    break;
                case OK:
                    CookieUtils cu = new CookieUtils();
                    if (remember.getObject()) {
                        cu.save(AUTH_CODE_COOKIE, login.getPerson().getToken());
                    } else {
                        cu.remove(AUTH_CODE_COOKIE);
                    }

                    BSession.get().setPerson(login.getPerson());
                    iReturn.onReturn(target);
                    break;
                default:
                    throw new IllegalArgumentException("Type not supported " + login.getType());
                }
            }
        });

        form.setDefaultButton((AjaxButton) form.get("ok"));

        form.add(new AjaxLink<Void>("forget") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                setResponsePage(ForgotPasswordPage.class);
            }

        });

        form.add(new CheckBox("remember", remember));

        target.focusComponent(email);
    }
}
